﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynamicProperties;

namespace ColoredStrings
{
	class Program
	{
		static DynamicProperty<string, ConsoleColor> dpStringColor = new DynamicProperty<string, ConsoleColor>("Color of string.");

		static void Main(string[] args)
		{
			// create names
			List<string> names = CreateNames();

			// write all names
			foreach(var name in names)
				WriteLine(name);
			
			Console.ReadLine();
		}

		private static List<string> CreateNames()
		{
			var names = new List<string>();
			for (int i = 0; i < 10; i++)
			{
				string name = "Name " + i;

				// set color for some names
				switch (i)
				{
					case 2:
						dpStringColor.Set(name, ConsoleColor.Blue);
						break;
					case 3:
						dpStringColor.Set(name, ConsoleColor.Green);
						break;
					case 4:
						dpStringColor.Set(name, ConsoleColor.Red);
						break;
					case 7:
						dpStringColor.Set(name, ConsoleColor.Yellow);
						break;
				}
				
				names.Add(name);
			}

			return names;
		}

		static void WriteLine(string str)
		{
			ConsoleColor oldColor = default(ConsoleColor);
			ConsoleColor color;
			bool changeColor = dpStringColor.TryGet(str, out color);
			if (changeColor)
			{
				// change color
				oldColor = Console.ForegroundColor;
				Console.ForegroundColor = color;
			}

			// write text
			Console.WriteLine(str);

			if (changeColor)
			{
				// restore old color
				Console.ForegroundColor = oldColor;
			}
		}
	}
}
